package com.example.myengine.rulehandler;

import com.example.myengine.dao.IRuleRepository;
import com.example.myengine.engine.EngineBaseProcess;
import com.example.myengine.model.TreeRuleRich;
import com.example.myengine.myrule.RuleFilterManager;
import com.example.myengine.request.RuleEngineResult;
import com.example.myengine.request.RuleExecuteReq;
import com.example.myengine.vo.TreeNodeVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

/**
 * @author ycd0075
 * @date 2024-02-04 10:19
 */
@Component("RuleEngineHandle")
public class RuleEngineHandle extends EngineBaseProcess {
    @Resource
    private IRuleRepository ruleRepository;

    @Resource
    private RuleFilterManager ruleFilterManager;

    @Override
    public RuleEngineResult process(RuleExecuteReq matter) {
        // 查询决策规则树
        TreeRuleRich treeRuleRich = ruleRepository.queryTreeRuleRich(matter.getTreeId());

        if (null == treeRuleRich) {
            throw new RuntimeException("Tree Rule is null!");
        }
        // 执行决策节点
        TreeNodeVO treeNodeInfo = engineDecisionMaker(treeRuleRich, matter, ruleFilterManager.getLogicFilterMap());
        // 返回决策结果
        return new RuleEngineResult(matter.getUserId(), treeNodeInfo.getTreeId(), treeNodeInfo.getTreeNodeId(), treeNodeInfo.getNodeValue());
    }
}
